package com.mj.listen1._02链表.circle.circle;

import com.mj.listen1._02链表.AbstractList;

/**
 * 双向循环链表
 *
 * @param <E>
 */
public class CircleLinkList<E> extends AbstractList<E> {

    private Node<E> first;
    private Node<E> last;
    private Node<E> current;

    private static class Node<E> {
        private E element;
        private Node<E> next;
        private Node<E> pre;

        public Node(Node<E> pre, E element, Node<E> next) {
            this.pre = pre;
            this.element = element;
            this.next = next;
        }
    }

    /**
     * 重置 current 到头节点
     */
    public void reset() {
        current = first;
    }

    /**
     * current 的下一个节点
     *
     * @return
     */
    public E next() {
        if (current == null) return null;
        current = current.next;
        return current.element;
    }

    /**
     * 删除 current 指向的节点
     *
     * @return
     */
    public E remove() {
        // 记录 current 的下一个节点
        Node<E> next = current.next;
        E element = remove(current);
        if (size == 0) {
            // 删除的是最后一个节点，需要特殊处理一下
            current = null;
        } else {
            // current 指向下一个节点
            current = next;
        }
        return element;
    }

    /**
     * 删除 node 所在的节点
     *
     * @param node
     * @return
     */
    private E remove(Node<E> node) {
        if (size == 1) {
            // 只有一个节点
            first = null;
            last = null;
        } else {
            // 原来要删除节点的上一个节点
            Node<E> pre = node.pre;
            // 原来要删除节点的下一个节点
            Node<E> next = node.next;
            pre.next = next;
            next.pre = pre;

            if (node == first) {// index = 0 <-> node == first
                first = next;
            }

            if (node == last) {// index = size - 1 <-> node == last
                last = pre;
            }
        }
        size--;
        return node.element;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        // 找到index位置的node，然后修改
        Node<E> node = node(index);
        node.element = element;
        return node.element;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == size) {// index = size，size = 0时候，表示还没有添加任何的节点
            Node<E> oldLast = last;
            last = new Node<>(oldLast, element, first);// next指向first
            if (oldLast == null) {
                // 这是链表添加的第一个元素
                first = last;
                // 指向自己
                first.next = first;
                first.pre = first;
            } else {
                oldLast.next = last;
                first.pre = last;
            }
        } else {
            // 要添加节点的索引
            Node<E> next = node(index);
            // 原来要添加节点的上一个节点
            Node<E> pre = next.pre;
            Node<E> newNode = new Node<>(pre, element, next);
            next.pre = newNode;
            pre.next = newNode;
            // 边界
            if (next == first) {// index = 0 <-> next == first
                first = newNode;
            }
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        return remove(node(index));
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = first;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (null == node) {
                    return i;
                }
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (element == node.element) {
                    return i;
                }
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    /**
     * 找到index位置的node
     *
     * @param index
     * @return
     */
    public Node<E> node(int index) {
        rangeCheck(index);
        if (index < size / 2) {
            Node<E> node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        } else {
            Node<E> node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.pre;
            }
            return node;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("size:").append(size).append("，[");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                sb.append("，");
            }
            sb.append(node.element);
            node = node.next;
        }
        sb.append("]");
        return sb.toString();
    }
}
